package com.dingqb.service;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;

public class ServiceD extends Service {

	private int count;
	public String DIVIDE_RESULT = "action_123";
	private boolean quit = false;

	@Override
	public void onCreate() {
		super.onCreate();
		new Thread(new Runnable() {
			@Override
			public void run() {
				while (!quit) {
					try {
						Thread.sleep(1000);
					} catch (InterruptedException e) {
					}
					count++;
					Log.i("CountService", count + "");
					if (count % 5 == 0) {
						Intent intent = new Intent(DIVIDE_RESULT);
						intent.putExtra("count", count);
						sendBroadcast(intent);
					}
				
				}
			}
		}).start();
	}

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		System.out.println("onstartCommand");
		return super.onStartCommand(intent, flags, startId);
	}

	@Override
	public IBinder onBind(Intent intent) {
		return new myBinder();
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		this.quit = true;
	}

	public class myBinder extends Binder implements ICountService {
		public void toStartPlay() {
			System.out.println("startpaly");
		}

		public void toStopPlay() {
			System.out.println("play");
		}

		public void toNext() {
			System.out.println("next");
		}

		public void toPrevious() {
			System.out.println("pre");
		}

		@Override
		public int getCount() {
			return count;
		}

	}

}
